package com.mm.serivce.taxi.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.domain.taxi.DTaxiAddressInfo;
import com.mm.domain.taxi.DTaxiOrderInfo;
import com.mm.domain.taxi.DUserOrderRelation;
import com.mm.mapper.taxi.DTaxiAddressInfoMapper;
import com.mm.mapper.taxi.DTaxiOrderInfoMapper;
import com.mm.mapper.taxi.DUserOrderRelationMapper;
import com.mm.serivce.taxi.IDTaxiOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mm.vo.mp.TaxiOrderRep;
import com.mm.vo.mp.TaxiOrderReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 打车订单表 服务实现类
 * </p>
 *
 * @author JueDream
 * @since 2021-08-04
 */
@Service
public class DTaxiOrderInfoServiceImpl extends ServiceImpl<DTaxiOrderInfoMapper, DTaxiOrderInfo> implements IDTaxiOrderInfoService {

    private final DTaxiOrderInfoMapper orderInfoMapper;

    private final DTaxiAddressInfoMapper addressInfoMapper;

    private final DUserOrderRelationMapper userOrderRelationMapper;

    /**
     * 引入 （平台）事务管理器，Spring 事务策略的核心。
     */
    @Autowired
    private PlatformTransactionManager transactionManager;

    public DTaxiOrderInfoServiceImpl(DTaxiOrderInfoMapper orderInfoMapper, DTaxiAddressInfoMapper addressInfoMapper, DUserOrderRelationMapper userOrderRelationMapper) {
        this.orderInfoMapper = orderInfoMapper;
        this.addressInfoMapper = addressInfoMapper;
        this.userOrderRelationMapper = userOrderRelationMapper;
    }

    @Override
    public Page<DTaxiOrderInfo> queryTaxiOrderPage(Integer page, Integer limit, String orderCode) {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        // 新发起一个事务
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        // 获得事务状态
        TransactionStatus status = transactionManager.getTransaction(def);
        try {
            Page<DTaxiOrderInfo> orderInfoPage = new Page<>(page, limit);
            LambdaQueryWrapper<DTaxiOrderInfo> queryWrapper = new LambdaQueryWrapper<DTaxiOrderInfo>();
            if (StrUtil.isNotBlank(orderCode)) {
                queryWrapper.like(DTaxiOrderInfo::getOrderCode, orderCode);
            }
            queryWrapper.eq(DTaxiOrderInfo::getDelFlg, '0');
            orderInfoPage = orderInfoMapper.selectPage(orderInfoPage, queryWrapper);
            Thread.sleep(10000);
            orderInfoPage.setTotal(orderInfoPage.getRecords().size());
            // 渲染地址信息
            Optional.ofNullable(orderInfoPage.getRecords()).orElse(new ArrayList<>())
                    .forEach(a -> {
                        if (a.getTaxiId() > 0) {
                            DTaxiAddressInfo addressInfo = addressInfoMapper.selectById(a.getTaxiId());
                            if (Objects.nonNull(addressInfo)) {
                                a.setTaxiAddress(addressInfo.getTaxiFirst() + "-" + addressInfo.getTaxiLast());
                            }
                        }
                    });
            // 手动提交事务
            transactionManager.commit(status);
            return orderInfoPage;
        } catch (Exception e) {
            // 手动回滚事务
            transactionManager.rollback(status);
        }
        return null;
    }

    @Override
    public DTaxiOrderInfo queryTaxiOrderOne(Integer orderId) {
        DTaxiOrderInfo orderInfo = new DTaxiOrderInfo();
        if (orderId > 0){
            orderInfo = orderInfoMapper.selectById(orderId);
        }
        return orderInfo;
    }

    @Override
    public boolean removeTaxiOrder(Integer orderId) {
        boolean flag = false;
        DTaxiOrderInfo orderInfo = orderInfoMapper.selectById(orderId);
        if (Objects.nonNull(orderInfo)) {
            orderInfo.setDelFlg(1);
            Integer result = orderInfoMapper.updateById(orderInfo);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }
}
